/*
 *  levelUpGame - Pickup.cpp
 *  Copyright 2011 W. Reckman. All rights reserved.
 */
#include "ofImage.h"
#include "LevelUpApp.h"
#include "Pickup.h"

const float Pickup::ANIM_SCALE_UP_FACTOR = 7.0f;
const float Pickup::ANIM_DURATION = 0.8f;
const float Pickup::COLLISION_SCALE = 0.2f;


Pickup::Pickup( const LevelUpApp& appRef_ )
: GridObject( appRef_, false ), img( 0 ), state( PST_ACTIVE )
{
	//setCollisionMode( GridObject::CM_CENTER );
	setCollisionBBoxScale( COLLISION_SCALE );
}


void Pickup::update() {
	if ( state == PST_DESTROY_ANIM ) {
		if ( animPos < 1.0f ) {
			float elapsed = ofGetElapsedTimef() - animStartTime;
			animPos = elapsed / ANIM_DURATION;
		} else {
			state = PST_DESTROYED;
		}
	}
}

void Pickup::draw() const {
	if ( img ) {
		const ofRectangle& coords = getBBox();
		float alpha = ( state == PST_ACTIVE ) ? 255 : 255.0f * ( 1.0f - animPos );
		float sf = ( state == PST_ACTIVE ) ? 1.0f : ( 1.0f + animPos * ( ANIM_SCALE_UP_FACTOR - 1.0f ) );
		float dw = coords.width * sf;
		float dh = coords.height * sf;
		
		ofPushStyle();
		ofSetColor( 255, 255, 255, alpha );
		
		img->draw( coords.x, coords.y, dw, dh );
		
		ofPopStyle();
		
		if ( appRef.debugDrawingEnabled ) {
			GridObject::draw();
		}
	}
}

Pickup::PICKUP_STATE Pickup::getState() const { return state; }
const ofImage* Pickup::getImage() const { return img; }
BALANCE_KIND Pickup::getBalanceType() const { return balanceType; }

void Pickup::setState( PICKUP_STATE state_ ) {
	state = state_;
	
	if ( state == PST_DESTROY_ANIM ) {
		animPos = 0.0f;
		setCollidable( false );
		animStartTime = ofGetElapsedTimef();
	}
}

void Pickup::setImage( ofImage* img_ ) {
	img = img_;
	setSize( img->getWidth(), img->getHeight() );
}

void Pickup::setBalanceType( BALANCE_KIND type_ ) { balanceType = type_; }
